<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: How to enable Halide backend for improve efficiency</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d2/d58/tutorial_table_of_content_dnn.html">Deep Neural Networks (dnn module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">How to enable Halide backend for improve efficiency </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d5/de7/tutorial_dnn_googlenet.html">Load Caffe framework models</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../de/d5d/tutorial_dnn_halide_scheduling.html">How to schedule your network for Halide backend</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Dmitry Kurtaev </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.3 </td></tr>
</table>
<h2>Introduction</h2>
<p>This tutorial guidelines how to run your models in OpenCV deep learning module using Halide language backend. Halide is an open-source project that let us write image processing algorithms in well-readable format, schedule computations according to specific device and evaluate it with a quite good efficiency.</p>
<p>An official website of the Halide project: <a href="http://halide-lang.org/">http://halide-lang.org/</a>.</p>
<p>An up to date efficiency comparison: <a href="https://github.com/opencv/opencv/wiki/DNN-Efficiency">https://github.com/opencv/opencv/wiki/DNN-Efficiency</a></p>
<h2>Requirements</h2>
<h3>LLVM compiler</h3>
<dl class="section note"><dt>Note</dt><dd>LLVM compilation might take a long time.</dd></dl>
<ul>
<li>Download LLVM source code from <a href="http://releases.llvm.org/4.0.0/llvm-4.0.0.src.tar.xz">http://releases.llvm.org/4.0.0/llvm-4.0.0.src.tar.xz</a>. Unpack it. Let <b>llvm_root</b> is a root directory of source code.</li>
<li>Create directory <b>llvm_root</b>/tools/clang</li>
<li>Download Clang with the same version as LLVM. In our case it will be from <a href="http://releases.llvm.org/4.0.0/cfe-4.0.0.src.tar.xz">http://releases.llvm.org/4.0.0/cfe-4.0.0.src.tar.xz</a>. Unpack it into <b>llvm_root</b>/tools/clang. Note that it should be a root for Clang source code.</li>
<li>Build LLVM on Linux <div class="fragment"><div class="line">cd llvm_root</div><div class="line">mkdir build &amp;&amp; cd build</div><div class="line">cmake -DLLVM_ENABLE_TERMINFO=OFF -DLLVM_TARGETS_TO_BUILD=&quot;X86&quot; -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_BUILD_TYPE=Release ..</div><div class="line">make -j4</div></div><!-- fragment --></li>
<li>Build LLVM on Windows (Developer Command Prompt) <div class="fragment"><div class="line">mkdir \\path-to-llvm-build\\ &amp;&amp; cd \\path-to-llvm-build\\</div><div class="line">cmake.exe -DLLVM_ENABLE_TERMINFO=OFF -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=\\path-to-llvm-install\\ -G &quot;Visual Studio 14 Win64&quot; \\path-to-llvm-src\\</div><div class="line">MSBuild.exe /m:4 /t:Build /p:Configuration=Release .\\INSTALL.vcxproj</div></div><!-- fragment --></li>
</ul>
<dl class="section note"><dt>Note</dt><dd><code>\\path-to-llvm-build\\</code> and <code>\\path-to-llvm-install\\</code> are different directories.</dd></dl>
<h3>Halide language.</h3>
<ul>
<li>Download source code from GitHub repository, <a href="https://github.com/halide/Halide">https://github.com/halide/Halide</a> or using git. The root directory will be a <b>halide_root</b>. <div class="fragment"><div class="line">git clone https://github.com/halide/Halide.git</div></div><!-- fragment --></li>
<li>Build Halide on Linux <div class="fragment"><div class="line">cd halide_root</div><div class="line">mkdir build &amp;&amp; cd build</div><div class="line">cmake -DLLVM_DIR=llvm_root/build/lib/cmake/llvm -DCMAKE_BUILD_TYPE=Release -DLLVM_VERSION=40 -DWITH_TESTS=OFF -DWITH_APPS=OFF -DWITH_TUTORIALS=OFF ..</div><div class="line">make -j4</div></div><!-- fragment --></li>
<li>Build Halide on Windows (Developer Command Prompt) <div class="fragment"><div class="line">cd halide_root</div><div class="line">mkdir build &amp;&amp; cd build</div><div class="line">cmake.exe -DLLVM_DIR=\\path-to-llvm-install\\lib\\cmake\\llvm -DLLVM_VERSION=40 -DWITH_TESTS=OFF -DWITH_APPS=OFF -DWITH_TUTORIALS=OFF -DCMAKE_BUILD_TYPE=Release -G &quot;Visual Studio 14 Win64&quot; ..</div><div class="line">MSBuild.exe /m:4 /t:Build /p:Configuration=Release .\\ALL_BUILD.vcxproj</div></div><!-- fragment --></li>
</ul>
<h2>Build OpenCV with Halide backend</h2>
<p>When you build OpenCV add the following configuration flags:</p>
<ul>
<li><code>WITH_HALIDE</code> - enable Halide linkage</li>
<li><code>HALIDE_ROOT_DIR</code> - path to Halide build directory</li>
</ul>
<h2>Set Halide as a preferable backend</h2>
<div class="fragment"><div class="line">net.setPreferableBackend(DNN_BACKEND_HALIDE);</div></div><!-- fragment --> </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:34 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
